﻿@using CleanArchitecture.Infrastructure.Entities.System.DataDic
@model IList<SystemDataDicTree>
@{
    Layout = "_BackendLayout";
    ViewData["Title"] = ViewBag.PageTitle as string;
    var RootDicNode = Model.First(d => d.ParentId == 0);
    int currentSystemModuleId = ViewBag.CurrentSystemModuleId;
}
@section cssstyles {
    <style type="text/css">
        body {
            margin: 20px !important;
            display: flex;
            flex: auto;
            flex-direction: column;
            min-height: 0;
            background: #f0f2f5;
        }
        .websitemanage {
            width: 100% !important;
            background-color: #fff;
            margin: 0 !important;
            padding: 10px 0;
        }
        .websitemanage .layui-nav-side {
            width: 100%;
            position: relative !important;
            color: #393D49 !important;
            background-color: #fff;
        }
        .layui-nav-tree .layui-this,
        .layui-nav-tree .layui-nav-item a:hover {
            background-color: #e6f7ff !important;
            color: #1890ff !important;
            font-weight: bold;
        }
        .layui-nav-tree .layui-this > a {
            background-color: inherit;
        }
        .layui-nav .layui-nav-item a {
            color: #393d49;
        }
        .layui-nav-tree .layui-nav-bar {
            background-color: #1890ff;
        }
        .layui-nav-tree .currentnav:after {
            transform: scaleY(1);
            opacity: 1;
            transition: transform .15s cubic-bezier(.645,.045,.355,1),opacity .15s cubic-bezier(.645,.045,.355,1);
            position: absolute;
            top: 0;
            left: auto;
            right: 0;
            bottom: 0;
            border-right: 3px solid #1890ff;
            height: 45px;
            content: "";
        }
    </style>
}
<div class="websitemanage layui-main">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md2">
            <ul class="layui-nav layui-nav-tree layui-nav-side" lay-filter="leftnav">
                @foreach (var site in Model.Where(d => d.ParentId != 0))
                {
                    <li class="layui-nav-item @(site.DataType.HasValue && site.DataType.Value == RootDicNode.DataType.Value ? "layui-this" : "")">
                        <a href="javascript:;" data-tab="/system/websitemanage/edit?datatype=@((int)site.DataType)">
                            @site.DisplayName
                            @if (site.DataType.HasValue && site.DataType.Value == RootDicNode.DataType.Value)
                            {
                                <strong>(默认)</strong>
                            }
                        </a>
                    </li>
                }
            </ul>
        </div>
        <div id="rightpanel" class="layui-col-md8" style="border-left: 1px solid #eaedf1;">
            <fieldset class="layui-elem-field">
                <div class="layui-field-box pagebuttongroup">
                    @await Component.InvokeAsync("PageButton")
                </div>
            </fieldset>
            <iframe id="clientIframe" src="" frameborder="0" scrolling="no" height="100%" width="100%"></iframe>
        </div>
    </div>
    <input type="text" class="layui-hide" id="currentSystemModuleId" value="@currentSystemModuleId" />
</div>
@section scripts{
    <script type="text/javascript">
        function reinitIframe() {
            var iframe = document.getElementById("clientIframe");
            try {
                var bHeight = iframe.contentWindow.document.body.scrollHeight;
                var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
                var height = Math.max(bHeight, dHeight);
                iframe.height = height;
                //console.log(height);
            } catch (ex) {
                console.error(ex);
            }
        }
        $(function () {
            'use strict';
            layui.use(['element', 'layer'], function () {
                var element = layui.element,
                    layer = layui.layer;
                var renderLeftNav = function ($thisElm) {
                    //layui在nav切换时清除了layui-this中的伪类after
                    //为了实现在当前选项卡中加上右边框费尽周折，必须加上！！
                    let $border = $('div.currentnav'),
                        loading = layer.load(2, { time: 2000 });
                    if (typeof $border !== "undefined") $border.remove();
                    $thisElm.after('<div class="currentnav"></div>');
                    $.wait(300).then(function () {
                        layer.close(loading);
                        $("#rightpanel>iframe").attr('src', $thisElm.data("tab"));
                    });
                    element.render('nav', 'leftnav');
                };
                //init Left Nav
                renderLeftNav($('.layui-nav-side .layui-this>a'));

                element.on('nav(leftnav)', function () {
                    renderLeftNav($(this));
                });

                window.οnresize = function () {
                    reinitIframe();
                };
                ////监听顶部按钮栏
                var PageButtonEvent = {
                    Save: function (moduleid, buttonid) {
                        //Button Authentication
                        let serveruri = '/api/system/websitemanage/checkbuttonauth?moduleid=' + moduleid + "&buttonid=" + buttonid;
                        httpapi.fetchGet(serveruri).then(res => {
                            if (!httpapi.checkResponse(res))
                                //输入信息校验错误
                                notice.error(res.message);
                            else {//Success
                                var iframe = document.getElementById("clientIframe");
                                var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
                                let iframesavebtn = $(innerDoc.body).find("#clientSettingsSubmit");
                                if (iframesavebtn.length > 0) {
                                    //触发iframe中保存按钮提交事件
                                    iframesavebtn.click();
                                }
                            }
                        }).catch(error => {
                            console.error(error);
                            notice.error("对不起，出现未知错误，请您联系系统管理员！");
                        });
                        return false;
                    }
                };
                $('.pagebuttongroup .layui-btn').on('click', function (e) {
                    e.preventDefault();
                    var type = $(this).data('type');
                    var buttonid = $(this).data("buttonid");
                    let currentSystemModuleId = $("#currentSystemModuleId").val();
                    PageButtonEvent[type] ? PageButtonEvent[type].call(this, currentSystemModuleId, buttonid) : '';
                });
            });
        });
    </script>
}
